package com.shangqing.system.droughtinformation.service.impl;

import com.shangqing.system.droughtinformation.entity.City;
import com.shangqing.system.droughtinformation.entity.DroughtCity;
import com.shangqing.system.droughtinformation.mapper.DroughtCityMapper;
import com.shangqing.system.droughtinformation.mapper.DroughtGradeMapper;
import com.shangqing.system.droughtinformation.service.IdroughtCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class DroughtCityImpl implements IdroughtCityService {
    @Autowired
    private DroughtCityMapper droughtCityMapper;
    @Autowired
    private DroughtGradeMapper droughtGradeMapper;

    /**
     * 获取在每个市的灾害数,计算的到每个市的占比
     */
    @Override
    public List<DroughtCity> getinfo(int year) {
        List<DroughtCity> droughtCityList = new ArrayList<>();
        List<DroughtCity> droughtCities = droughtCityMapper.getinfo(year);
        int sum = droughtCityMapper.getNumberSum();
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        for (DroughtCity d : droughtCities) {
            int city = d.getNumber();
            String accounted = numberFormat.format((float) city / (float) sum * 100) + "%";
            d.setAccounted(accounted);
            d.setDroughtGrade(droughtGradeMapper.getinfoBydroughtGradeid(d.getDroughtGradeid()));
            droughtCityList.add(d);
        }
        return droughtCityList;
    }

    /**
     * 得到灾害数的总和
     *
     * @return
     */
    @Override
    public int getcitySum() {
        return droughtCityMapper.getNumberSum();
    }

    /**
     * 得到所有地区信息
     */
    @Override
    public City getAllCity(int id) {
        return droughtCityMapper.getAllCity(id);
    }
}
